var particleQtComp;
var snowBalls = new Array();

var gravitation = 1;
var pixelForce = 0.04;


var screenWidth, screenHeight;
var snowmanXcoordMax;
var lastThrow;

var myLifes = 5, enemyLifes = 5;

var isServer = false;

var gameOver = 1;

function gameStart(screen)
{
    screenWidth = screen.width;
    screenHeight = screen.height;
    snowmanXcoordMax = 335;
    console.log("Start game: w="+screenWidth+" h="+screenHeight);
    //lblLifesCount.text = myLifes;
    //lblEnemyLifesCount.text = enemyLifes;

    enemySnowman.x = (screenWidth-snowman.x-snowman.width);
}

function looseLife()
{
    myLifes--;
    lblLifesCount.text = myLifes;
    if(myLifes==0)
    {
        screen.parent.pageStack.push(Qt.resolvedUrl("LosePage.qml"));
    }
}

function enemyLooseLife()
{
    enemyLifes--;
    lblEnemyLifesCount.text = enemyLifes;
    if(enemyLifes==0){
        screen.parent.pageStack.push(Qt.resolvedUrl("WinPage.qml"));
    }
}

function setScoreInfo(my, enemy) {
    myLifes = my;
    enemyLifes = enemy;

    lblLifesCount.text = myLifes;
    lblEnemyLifesCount.text = enemyLifes;
}

function checkScores() {
    if(gameOver == 1) {
        if(myLifes < 1)
        {
            screen.parent.pageStack.push(Qt.resolvedUrl("LosePage.qml"));
            gameOver = 2;
            return;
        }

        if(enemyLifes < 1){
            screen.parent.pageStack.push(Qt.resolvedUrl("WinPage.qml"));
            gameOver = 2;
            return;
        }
    }
}

function setLastThrow(pinch)
{
    var horizSpeed = Math.abs(pinch.point2.x - pinch.point1.x)*pixelForce;
    var vertSpeed = Math.abs(pinch.point2.y - pinch.point1.y)*pixelForce;

    console.log("ThrowBall: x="+pinch.point1.x + " y=" +pinch.point1.y);
    console.log("horizSpeed= " + horizSpeed + " vertSpeed= " +vertSpeed );

    lastThrow = {horizSpeed: horizSpeed, vertSpeed:vertSpeed};
}

function ThrowBall(tParent) {

    console.log(tParent);
    var newsnowBall = Qt.createComponent("SnowballObject.qml");
    var snb = newsnowBall.createObject(tParent,
                                       {x:snowman.x,y:snowman.y});


    console.log("ThrowBall horizSpeed= " + lastThrow.horizSpeed + " vertSpeed= " + lastThrow.vertSpeed );
    snowBalls.push({sBall:snb, xSpeed:lastThrow.horizSpeed, ySpeed:lastThrow.vertSpeed, isEnemy:false});

    BluetoothManager.throwBall(screenWidth-snowman.x-snowman.width+(snowman.width-32), snowman.y, -lastThrow.horizSpeed, lastThrow.vertSpeed);
}

function ThrowEnemyBall(positionX, positionY, speedX, speedY)
{
    var newsnowBall = Qt.createComponent("SnowballObject.qml");
    var snb = newsnowBall.createObject(screen,
                                       {x:positionX,y:positionY,source:"images/enemy_snowball.png"});

    var horizSpeed = speedX;
    var vertSpeed = speedY;

    console.log("Enenmy snb horizSpeed= " + horizSpeed + " vertSpeed= " +vertSpeed );

    snowBalls.push({sBall:snb, xSpeed:horizSpeed, ySpeed:vertSpeed, isEnemy:true});
}

function moveSnowball(snb,i)
{
    snb.sBall.x += snb.xSpeed;
    snb.sBall.y -= snb.ySpeed;
    snb.ySpeed -= gravitation;    

    if((snb.sBall.x>400 && snb.sBall.x<450) && snb.sBall.y > 335)
    {
        //console.log("barrier x"+ snb.sBall.x + " y"+snb.sBall.y);
        destroySnowball(snb, i);
        return;
    }

    if(snb.sBall.x>screenWidth -10 || snb.sBall.y>screenHeight-10)
    {
        destroySnowball(snb, i);
        return;
    }

    if(DetectHit(snb)===true)
    {
        destroySnowball(snb, i);
    }
}

function destroySnowball(snb, i)
{
    showParticles(snb.sBall.x, snb.sBall.y);
    snowBalls.splice(i,1);
    snb.sBall.destroy();
}

function DetectHit(snb)
{
    var isEnemy = false;
    var targetSnowman = enemySnowman;
    var msg = "enemy";

    if(snb.isEnemy === true)
    {
        targetSnowman = snowman;
        isEnemy = true;
        msg = "ouch";
    }

    //console.log("Hit " + msg);

    if(snb.sBall.x>targetSnowman.x && snb.sBall.x<targetSnowman.x+targetSnowman.width &&
            snb.sBall.y>targetSnowman.y &&snb.sBall.y<targetSnowman.y+targetSnowman.height)
    {
        if(isEnemy===true)
        {
            looseLife();
        }
        else
        {
            enemyLooseLife();
        }

        return true;
    }
    return false;
}

function timeTick()
{
    animateSnowBalls();
    moveSnowman();
}

function animateSnowBalls()
{
    for(var i=0; i<snowBalls.length; i++)
    {
        moveSnowball(snowBalls[i], i);
    }
}

function moveSnowman()
{
    var accelReading = accel.reading;
    if(Math.abs(accelReading.y)>3)
    {
        snowman.x += accelReading.y;
    } else {
        return;
    }

    if(snowman.x<0)
    {
        snowman.x = 0;
    }

    if(snowman.x>snowmanXcoordMax )
    {
        snowman.x = snowmanXcoordMax;
    }

    //console.log("Snowman poz: " + snowman.x);
    BluetoothManager.updatePosition(screenWidth-snowman.x-snowman.width, snowman.y);

}

function updateScoreInformation() {
    BluetoothManager.updateScores(myLifes, enemyLifes);
}

function showParticles(x, y)
{
    particleQtComp = Qt.createComponent("ParticleObject.qml");
    var particle = particleQtComp.createObject(screen, {"x":x, "y":y});
    //console.log("part x= " + x + " y=" +y);
}

function setServerMode() {
    console.log("Server mode set to TRUE!");
    isServer = true;
}
